﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EntityAndDomainLibrary.DomainModel;
using EntityAndDomainLibrary.EntityModel;
using Log4NetLibrary;

namespace MeterServiceClientApp.UC
{
    public partial class ucAllMetersLatestUpdate : UserControl
    {

        #region Private Variables
        DataBaseAccess dbaccess = new DataBaseAccess();
        ILogService logService;

        int? PreviousX = null;
        int? PreviousY = null; 
        #endregion

        public ucAllMetersLatestUpdate()
        {
            try
            {
                log4net.Config.XmlConfigurator.Configure();
                logService = new FileLogService(typeof(ucAllMetersLatestUpdate));

                InitializeComponent();
                LoadData();
            }


            catch (Exception ex)
            {
                logService.Error(ex.Message);
            }
        }

        private void LoadData()
        {
            try
            {
                IList<MeterDataDisplayed> _listdata = dbaccess.GetLastUpdatedTouData();

                if (_listdata != null)
                {
                    int meters = 0;
                    lblLastUpdate.Text = dbaccess.GetLatestDate().ToString();
                    foreach (MeterDataDisplayed displaydata in _listdata)
                    {

                        meters++;

                        if (displaydata != null)
                        {
                            ucMeterData uc = new ucMeterData();
                            uc.LoadData(displaydata);

                            this.pnl.Controls.Add(uc);

                            if (meters <= 6)
                            {
                                if (uc.Location.X == 0)
                                    uc.Location = new System.Drawing.Point(
                                                     Convert.ToInt32(PreviousX + 120),
                                                     Convert.ToInt32(PreviousY));

                                PreviousX = uc.Location.X;
                                PreviousY = uc.Location.Y;
                            }

                            else
                            {
                                if (meters == 7)
                                {
                                    PreviousX = null;
                                    PreviousY = 220;
                                }

                                uc.Location = new System.Drawing.Point(
                                                     Convert.ToInt32(PreviousX + 120),
                                                     Convert.ToInt32(PreviousY));

                                PreviousX = uc.Location.X;
                                PreviousY = uc.Location.Y;

                            }

                        }
                    }
                }
            }

            catch (Exception ex)
            {
                logService.Error(ex.Message);
            }

        }
        
    }
}
